/*
* Vector2.cpp
*
*  Created on: Feb 17, 2011
*      Author: RRam
*/

#include "Vector2.h"
#include <math.h>


namespace GSP280Project3A
{//C004: COPY CONSTRUCTOR
	Vector2::Vector2(const Vector2 &vect2) 
	{
		this->x = vect2.x;
		this->y = vect2.y;
	}

	//C005: ASSIGNMENT OPERATOR
	Vector2 & Vector2::operator= ( const Vector2 &vect2 )
	{
		//check for self assignment
		if ( this != &vect2 )
		{
			this->x = this->y = 0; //clear member contents

			this->x = vect2.x;
			this->y = vect2.y;
		}
		//
		return *this;
	}

	//M001: Set Location
	void Vector2::XY(const Vector2 &vect2) 
	{
		this->x = vect2.x;
		this->y = vect2.y;
	}

	//M002: Set x-value, y-value
	void Vector2::XY(const float &_x, const float &_y) 
	{
		this->x = _x;
		this->y = _y;
	}

	//M003: Get Location
	const Vector2 &Vector2::XY() const 
	{
		return (Vector2(this->x, this->y));
	}

	//M004: Set x-value
	void Vector2::X(const float &_x)
	{
		this->x = _x;
	}

	//M005: Get x-value
	const float &Vector2::X() const
	{
		return (this->x);
	}

	//M006: Set y-value
	void Vector2::Y(const float &_y)
	{
		this->y = _y;
	}

	//M007: Get y-value
	const float &Vector2::Y() const
	{
		return (this->y);
	}

	//M008: Addition operator
	const Vector2 Vector2::operator +(const Vector2 &r)
	{
		return ( Vector2( (this->x + r.x), (this->y + r.y) ) );
	}

	//M009: Subtraction operator
	const Vector2 Vector2::operator -(const Vector2 &r)
	{
		return ( Vector2( (this->x - r.x), (this->y - r.y) ) );
	}

	//M010: Multiplication operator
	const Vector2 Vector2::operator *(const float &scalar)
	{
		return ( Vector2( (this->x * scalar), (this->y * scalar) ) );
	}

	//M011: Convert Location from +ve to -ve
	const Vector2 Vector2::operator -() const
	{
		return ( Vector2( (-this->x), (-this->y) ) );
	}

	//M012: Increment Location by a given vector
	const Vector2 & Vector2::operator +=(const Vector2 &r)
	{
		this->x += r.x;
		this->y += r.y;

		return *this;
	}

	//M013: Decrement Location by a given vector
	const Vector2 &Vector2::operator -=(const Vector2 &r)
	{
		this->x -= r.x;
		this->y -= r.y;

		return *this;
	}

	const float Vector2::magnitude() const
	{
		return ( sqrt((this->x * this->x) + (this->y * this->y)) );
	}

	Vector2 Vector2::normalize()
	{
		return ( Vector2((this->x / magnitude()), (this->y / magnitude())) );
	}

	const float Vector2::dotProduct(Vector2 const &vec1)
	{
		Vector2 vec2;
		vec2.x = this->x * vec1.x;
		vec2.y = this->y * vec2.y;

		// returns a scalar
		return (vec2.x + vec2.y);
	}

}//namespace
